package org.example.controller;

import org.example.entity.CategoryEntity;
import org.example.exception.UserException;
import org.example.service.ICategoryService;
import org.example.util.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author hanjiafeng
 * @date 2021/4/20 10:12
 * @desc todo
 */
@Controller
@RequestMapping("category")
public class CategoryController {

    private final ICategoryService categoryService;

    public CategoryController(ICategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("list")
    @ResponseBody
    public ResponseEntity<List<CategoryEntity>> list() {
        ResponseEntity<List<CategoryEntity>> responseEntity;
        try {
            responseEntity = ResponseEntity.success("读取商品列表成功");
            List<CategoryEntity> categoryEntityList = categoryService.list();
            responseEntity.setData(categoryEntityList);
        } catch (UserException e) {
            e.printStackTrace();
            responseEntity = ResponseEntity.failure("读取商品列表失败:" + e.getMessage());
        }
        return responseEntity;
    }
}
